package com.example.its2021606134chh.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.example.its2021606134chh.ThresholdModule.ThresholdParams;
import com.google.gson.Gson;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LifeAssistantFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LifeAssistantFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private TextView tvLifeAPM25;
    private TextView tvLifeATemperature;
    private TextView tvLifeAHumidity;
    private TextView tvLifeASunTitle;
    private TextView tvLifeASun;
    private TextView tvLifeARunTitle;
    private TextView tvLifeARun;
    private Timer timer;
    private TimerTask timerTask;

    public LifeAssistantFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment LifeAssistantFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static LifeAssistantFragment newInstance(String param1, String param2) {
        LifeAssistantFragment fragment = new LifeAssistantFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_life_assistant, container, false);
        // Inflate the layout for this fragment
        tvLifeAPM25 = (TextView) view.findViewById(R.id.tvLifeAPM25);
        tvLifeATemperature = (TextView) view.findViewById(R.id.tvLifeATemperature);
        tvLifeAHumidity = (TextView) view.findViewById(R.id.tvLifeAHumidity);
        tvLifeASunTitle = (TextView) view.findViewById(R.id.tvLifeASunTitle);
        tvLifeASun = (TextView) view.findViewById(R.id.tvLifeASun);
        tvLifeARunTitle = (TextView) view.findViewById(R.id.tvLifeARunTitle);
        tvLifeARun = (TextView) view.findViewById(R.id.tvLifeARun);
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onFailure(Exception e) {

                    }

                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        ThresholdParams params = gson.fromJson(json, ThresholdParams.class);
                        int Temperature = Integer.parseInt(params.getTemperature());
                        int Humidity = Integer.parseInt(params.getHumidity());
                        int Pm25 = Integer.parseInt(params.getPm25());
                        int LightIntensity = Integer.parseInt(params.getLightIntensity());
//                        Log.i("TAG", ""+LightIntensity);
                        tvLifeATemperature.setText(String.valueOf(Temperature));
                        tvLifeAHumidity.setText(String.valueOf(Humidity));
                        tvLifeAPM25.setText(String.valueOf(Pm25));
                        if (Pm25 >= 0 && Pm25 < 100) {
                            tvLifeARunTitle.setText("良好");
                            tvLifeARunTitle.setTextColor(Color.BLACK);
                            tvLifeARun.setText("气象条件会对晨练影响不大。");
                        }
                        if (Pm25 >= 100 && Pm25 < 200) {
                            tvLifeARunTitle.setText("轻度");
                            tvLifeARunTitle.setTextColor(Color.BLACK);
                            tvLifeARun.setText("受天气影响，较不宜晨练。");
                        }
                        if (Pm25 >= 200 && Pm25 < 300) {
                            tvLifeARunTitle.setText("重度");
                            tvLifeARunTitle.setTextColor(Color.RED);
                            tvLifeARun.setText("减少外出，出行注意戴口罩。");
                        }
                        if (Pm25 >= 300) {
                            tvLifeARunTitle.setText("爆表");
                            tvLifeARunTitle.setTextColor(Color.RED);
                            tvLifeARun.setText("停止一切外出活动。");
                        }
                        if (LightIntensity >= 0 && LightIntensity < 2000) {
                            tvLifeASunTitle.setText("非常弱");
                            tvLifeASunTitle.setTextColor(Color.BLACK);
                            tvLifeASun.setText("您无须担心紫外线。");
                        }
                        if (LightIntensity >= 2000 && LightIntensity <= 3000) {
                            tvLifeASunTitle.setText("弱");
                            tvLifeASunTitle.setTextColor(Color.BLACK);
                            tvLifeASun.setText("外出适当涂抹低倍数防晒霜。");
                        }
                        if (LightIntensity > 3000) {
                            tvLifeASunTitle.setText("强");
                            tvLifeASunTitle.setTextColor(Color.RED);
                            tvLifeASun.setText("外出需要涂抹中倍数防晒霜。");
                        }
                    }
                }, "get_all_sense", new String[]{"UserName"}, "user1");
            }
        };
        timer.schedule(timerTask, 0, 3000);

        return view;
    }

    private void initView() {

    }
}